window.onload = function() {
	var oNum = 1;
	//查询块高度
	var queryChannelInfoUrl = '/api/public/queryChannelInfo';
	var queryChannelInfoData = {
		"RequestType": "QueryChannelInfoReq",
		"ChannelId": "mychannel"
	};
	$.ajax({
		type: "POST",
		contentType: "application/json",
		url: queryChannelInfoUrl,
		data: JSON.stringify(queryChannelInfoData),
		dataType: "json",
		success: function(data) {
			console.log(data);
			if(data.data.length <= 0) {
				showHtml();
			} else {
				var oData = JSON.parse(data.data);
				if(oData.Code == -1) {
					showHtml()
				} else if(oData.Code == 0) {
					var oLength = Math.ceil(oData.ChannelInfo.Heigth / 5);
					var str = '<div id="position"><div class="positionBox clear"><div class="positionLeft fl">共' + (oData.ChannelInfo.Heigth - 1) + '条</div><ul id="positionNav"><li class="previousPage">上一页</li>';

					for(var i = 0; i < oLength; i++) {
						if(oLength >= 5) {
							str += '<li></li><li></li><li></li><li></li><li></li>';
							break;
						} else {
							str += '<li></li>';
						}

					}

					str += '<li class="nextPage">下一页</li></ul><div class="positionRt fl"><span>前往</span><input type="text" value="" class="number"/><span>页</span><input type="button" value="确认" class="confirmBtn"/></div></div>';
					$(".page").append(str);

					oPage();
				}
			}

		},
		error: function(XMLHttpRequest, textStatus, errorThrown) {
			showHtml();
			$(".cover").hide();
			console.log("调取sns接口出错");
			console.log(XMLHttpRequest.status);
			console.log(XMLHttpRequest.readyState);
			console.log(textStatus);
		}
	});

	function oPage() {
		var oPositionNav = document.getElementById('positionNav');
		var li = oPositionNav.getElementsByTagName('li');
		li[1].className = 'active';
		var num01 = 1;
		var Total = 800 /*总数*/ ;

		$(".confirmBtn").click(function() {
			Background(1);
			num01 = $(".number").val();
			console.log(num01)
			
			$(".cover").show();
			if(num01 == 1) {
				var num = parseInt(num01);
			} else {
				var num = ((num01-1) * 5) + 1;
				
			}
//			content(num01);
			pageData(num,num01,1);

		})

		//3 //上一页的点击事件
		li[li.length - li.length].onclick = function() {
			$(".nextPage").removeClass("BtnDisabled");
			for(var j = 0; j < li.length - 2; j++) {
				if(li[j + 1].className == 'active' && li[j + 1].innerHTML != 1) {
					if(j + 1 != li.length - (li.length - 1)) {
						Background(j);

						oNum--;
//						content(oNum);
						console.log('上一页点击：' + oNum);
						$(".cover").show();
						pageData(((oNum - 1) * 5) + 1)
					}
					break;
				}
			}
			if(j + 1 == li.length - 1) {
				num01=1;
//				content(num01);
				//				console.log('上一页点击：'+num01+'---------')
				if($(".previousPage").hasClass("BtnDisabled")){
					return false;
				}
				$(".cover").show();
				pageData(num01);
			}
		}
		//4 下一页的点击事件
		li[li.length - 1].onclick = function() {
			$(".previousPage").removeClass("BtnDisabled");
			for(var j = 0; j < li.length; j++) {
				if(li[j].className == 'active' && li[j].innerHTML < Total) { //* && 写最后一页的总数*/
					if(j + 1 < li.length - 1) {
						Background(j);

						oNum++;
						$(".cover").show();
						console.log('下一页点击：' + oNum);
						pageData(((oNum - 1) * 5) + 1,oNum,1);
					}
					break;
				}
			}
			if(j + 1 == li.length - 1) {
				num01++;
				oNum++;
				//				console.log("下一页点击："+oNum+'-----------');
				
				if($(".nextPage").hasClass("BtnDisabled")){
					return false;
				}
				$(".cover").show();
				Background(j-3);
				pageData(((oNum - 1) * 5) + 1,oNum,2);
			}
		}
		//     分页的点击事件
		for(var i = 0; i < li.length - 2; i++) {
			li[i + 1].index = i;
			li[i + 1].onclick = function() {
				$(".previousPage").removeClass("BtnDisabled");
				$(".nextPage").removeClass("BtnDisabled");
				Background(this.index+1);

				oNum = this.innerHTML;
				$(".cover").show();
								console.log("点击："+oNum);
				pageData(((oNum - 1) * 5) + 1,oNum,0);
			}
		}
		//把所有的分页背景去掉，给指定的分页添加背景颜色
		function Background(num) {
			console.log(num+'-----------')
			for(var i = 0; i < li.length; i++) {
				li[i].className = li[i].className.replace('active', '');
				li[num].className = 'active';
			}
		}
		// 给li 写内容
		content(num01);

		

	}

	function content(number) {
		var oPositionNav = document.getElementById('positionNav');
		var li = oPositionNav.getElementsByTagName('li');
//		li[1].className = 'active';
		for(var i = 0; i < li.length - 2; i++) {
			li[i + 1].innerHTML = number;
			number++;
		}
	}
	
	var oUrl = '/api/public/queryBlock';

	function pageData(StartIndex,pageNum,oNow) {
		var queryBlockoData = {
			"RequestType": "QueryBlockReq",
			"ChannelId": "mychannel",
			"QueryBlockInfo": {
				"QueryType": 1,
				"StartIndex": StartIndex,
				"Count": 5
			}
		};
		$.ajax({
			type: "POST",
			contentType: "application/json",
			url: oUrl,
			data: JSON.stringify(queryBlockoData),
			dataType: "json",
			success: function(data) {
				$(".cover").hide();
				var oData = JSON.parse(data.data);
				if(oData.Code==-1){
					alert("暂无数据");
					$(".nextPage").addClass("BtnDisabled");
					return false;
				}
				
				if(oNow==0){
					console.log(1111111)
				}else if(oNow==1){
					content(pageNum);
				}else{
					console.log(pageNum+'+++++++++++')
					content(pageNum);
				}
				
				console.log(oData);
				if(oData.BlockInfoList.length<5){
					$(".nextPage").addClass("BtnDisabled");
				}
				
//				console.log()
				if(StartIndex==1){
					$(".previousPage").addClass("BtnDisabled");
//					return false;
				}
//				content(pageNum);
				nodeStr(oData);
			},
			error: function(XMLHttpRequest, textStatus, errorThrown) {
				showHtml()
				$(".cover").hide();
				console.log("调取sns接口出错");
				console.log(XMLHttpRequest.status);
				console.log(XMLHttpRequest.readyState);
				console.log(textStatus);
			}
		});
	}

	function nodeStr(oData) {
		if(oData.Code == -1) {
			if($(".nextPage").hasClass("BtnDisabled")){		
				return false;
			}else{
				$(".nextPage").addClass("BtnDisabled");
				oNum--;
				alert("暂无数据!");				
			}
			
		} else if(oData.Code == 0) {
			var str = '';
			for(var i = 0; i < oData.BlockInfoList.length; i++) {
				str += '<div class="blockListCt clear"><div class="col-xs-2"><span>' + oData.BlockInfoList[i].BlockNum + '</span></div><div class="col-xs-8 text-center"><a>' + oData.BlockInfoList[i].CurrentBlockHash + '</a></div><div class="col-xs-2 text-right"><em>' + oData.BlockInfoList[i].TransactionList.length + '</em></div></div>';
			}
			$(".blockList").html(str);
		}

	}
	
	function showHtml(){
		$(".blockBox").html('<div class="NoData">暂无数据</div>');
		$(".NoData").show();
	}
	
}